package com.demo.core.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.base.model.UserInfo;
import com.demo.base.util.CommonUtil;
import com.demo.core.service.UserInfoService;

@RequestMapping("/userInfo")
@Controller
public class UserInfoController {

	@Autowired
	private UserInfoService userInfoService;
	
	/**
	 * 添加用户
	 * @param userInfo
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addUserInfo")
	public String addUserInfo(@RequestBody UserInfo userInfo){
		
		try {
			userInfoService.addUserInfo(userInfo);
			return "添加成功";
		} catch (Exception e) {
			return "添加失败";
		}
		

	}
	
	/**
	 * 修改用户
	 * @param userInfo
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateUserInfo")
	public String updateUserInfo(@RequestBody UserInfo userInfo){
		
		try {
			userInfoService.updateUserInfo(userInfo);
		} catch (Exception e) {
			
			return CommonUtil.FAIL;
		}
		
		return CommonUtil.SUCCESS;
	}
	
	/**
	 * 删除用户
	 * @param userInfo
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteUserInfo")
	public String deleteUserInfo(@RequestBody UserInfo userInfo){
		try {
			userInfoService.deleteUserInfo(userInfo);
		} catch (Exception e) {
			return CommonUtil.FAIL;
		}
	
		return CommonUtil.SUCCESS;
	}
	
	/**
	 * 获取用户信息
	 * @param userInfo
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getUserInfo")
	public Map<String,Object> getUserInfo(@RequestBody UserInfo userInfo){
		
		Map<String,Object> map = new HashMap<String,Object>();
		//验证用户ID是否为空
		if(CommonUtil.isNull(userInfo.getId())){
			map.put(CommonUtil.ERROR, "用户Id不能空");
			return map;
		}
		
		 userInfo = userInfoService.getUserInfo(userInfo.getId());
		 
		 //判断获取的用户是否为空
		 if(CommonUtil.isNull(userInfo)){
			 map.put(CommonUtil.ERROR, "用户不存在！");
		 }else{
			 map.put("user", userInfo);
		 }
		 
		 return map;
	}
}
